package com.bawei.caohao20240805.view.activity

import android.view.View
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import coil.load
import com.alipay.sdk.app.EnvUtils
import com.alipay.sdk.app.PayTask
import com.ayvytr.adapter.smart
import com.bawei.caohao20240805.MyApp
import com.bawei.caohao20240805.R
import com.bawei.caohao20240805.base.BaseActivity
import com.bawei.caohao20240805.databinding.ActivityTbactivityBinding
import com.bawei.caohao20240805.viewmodel.ZhiViewModel
import com.bawei.comm.model.bean.LiveBean
import com.bawei.comm.model.util.socket.WebSocketUtils
import com.kd.easybarrage.Barrage
import com.shuyu.gsyvideoplayer.GSYVideoManager


class ZBActivity : BaseActivity<ZhiViewModel,ActivityTbactivityBinding>(R.layout.activity_tbactivity) {
    var money=1000
    var lvadapter=
        smart(mutableListOf<LiveBean.Result>(),R.layout.lv_mo,{item, position ->
            findViewById<TextView>(R.id.name).text=item.name
            findViewById<TextView>(R.id.money).text="${item.price}"
            findViewById<ImageView>(R.id.img).load(item.imageUrl)

        }) {  }.click { item, position ->
            if(money<=0){
                AlertDialog.Builder(this)
                    .setTitle("充值中心")
                    .setMessage("余额不足")
                    .setNegativeButton("取消充值",null)
                    .setPositiveButton("充值",{wwww,www->
                        mViewModel.getpay(1000)
                    })
                    .show()
            }else{
                money=money-item.price
                mDing.imgDong.load(item.imageUrl)
                var animation = AnimationUtils.loadAnimation(this, R.anim.hua)
                mDing.imgDong.visibility=View.VISIBLE
                mDing.imgDong.animation=animation
                animation.setAnimationListener(object :AnimationListener{
                    override fun onAnimationStart(animation: Animation?) {

                    }

                    override fun onAnimationEnd(animation: Animation?) {
//                    TODO("Not yet implemented")
                        mDing.imgDong.visibility=View.GONE
                    }

                    override fun onAnimationRepeat(animation: Animation?) {
//                    TODO("Not yet implemented")
                    }

                })
            }

        }
    override fun dataObSer() {
        mViewModel.chodata.observe(this, Observer {

                val orderInfo: String = it.data.orderinfo // 订单信息

                val payRunnable = Runnable {
                    val alipay = PayTask(this)
                    val result = alipay.payV2(orderInfo, true)
                    money=money+1000
                }
                // 必须异步调用
                // 必须异步调用
                val payThread = Thread(payRunnable)
                payThread.start()

        })
        mViewModel.kandata.observe(this, Observer {
            if(it.result!=null){
                mDing.img.load(it.result.headPic)
                mDing.title.text=it.result.nickName
                mDing.sg.setUp(it.result.liveStreamUrl,false,"")
                mDing.sg.startPlayLogic()
                www.startConnect(it.result.barrageWsUrl,this)
            }
        })
        mViewModel.livedata.observe(this, Observer {
            if(it.result!=null){
                lvadapter.update(it.result,true)
            }
        })
    }
    lateinit var www :WebSocketUtils
    override fun initView() {
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        mViewModel.getlive()
        mDing.recLv.adapter=lvadapter
        mDing.recLv.layoutManager=GridLayoutManager(this,3)
        mDing.gify.setOnClickListener {
            if(mDing.recLv.isVisible){
                mDing.recLv.visibility=View.GONE
            }else{
                mDing.recLv.visibility=View.VISIBLE
            }
        }
        www=WebSocketUtils()
        www.setOnMessageListener {
            mDing.barr.addBarrage(Barrage(it))
        }
        var id = intent.getIntExtra("id", 0)
        var type = intent.getIntExtra("type", 0)
        if(type==1){
            mViewModel.getkan(MyApp.userId,MyApp.sessionId,id)
        }else{
            var url="http://10.59.9.24//live-platform/recordVideo/${id}"
            mDing.sg.setUp(url,false,"")
            mDing.sg.startPlayLogic()
        }
        mDing.fa.setOnClickListener {
            var toString = mDing.nei.text.toString()
            mDing.barr.addBarrage(Barrage("康景威："+toString))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }
}